package com.first.nell.home

import android.text.SpannableString
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Transformations
import com.first.nell.ProjectApplication
import com.first.nell.base.BaseViewModel
import com.first.nell.data.ReqBase
import com.first.nell.extend.isFirstNellAgreeFirstNellSpecFirstNellPermissions
import com.first.nell.network.ApiUserReposity
import com.first.nell.utils.AppConfigUtil

class HomeViewModel : BaseViewModel() {

    private val mFirstNellFetchFirstNellHomeFirstNellCreditFirstNellLiveData =
        MutableLiveData<ReqBase>()

    val onFirstNellFetchFirstNellNextFirstNellCreditFirstNellPurpleFirstNellLiveData =
        MutableLiveData<SpannableString>()
    val onFirstNellFetchFirstNellNextFirstNellCreditFirstNellRedFirstNellLiveData =
        MutableLiveData<SpannableString>()
    val onFirstNellIsFirstNellNeedFirstNellToFirstNellRequireFirstNellPermissionsFirstNellLiveData =
        MutableLiveData<Array<String>>()

    val onFirstNellFetchFirstNellHomeFirstNellCreditFirstNellLiveData =
        Transformations.switchMap(mFirstNellFetchFirstNellHomeFirstNellCreditFirstNellLiveData) { query ->
            ApiUserReposity.fetchFirstNellHomeFirstNellCredit(this, query)
        }

    fun fetchFirstNellHomeFirstNellCredit(reqBase: ReqBase) {
        mFirstNellFetchFirstNellHomeFirstNellCreditFirstNellLiveData.value = reqBase
    }

    fun isFirstNellNeedFirstNellToFirstNellRequireFirstNellPermissoins() {
        //判断下权限页权限
        val permissionList =
            AppConfigUtil.getFirstNellPermissionsFirstNellOfFirstNellHomeFirstNellPage()
        if (permissionList.isNullOrEmpty()) {
            onFirstNellIsFirstNellNeedFirstNellToFirstNellRequireFirstNellPermissionsFirstNellLiveData.value =
                arrayOf()
        } else {
            val isAgreePermissions =
                ProjectApplication.context.isFirstNellAgreeFirstNellSpecFirstNellPermissions(
                    permissionList
                )
            if (isAgreePermissions) {
                onFirstNellIsFirstNellNeedFirstNellToFirstNellRequireFirstNellPermissionsFirstNellLiveData.value =
                    arrayOf()
            } else {
                onFirstNellIsFirstNellNeedFirstNellToFirstNellRequireFirstNellPermissionsFirstNellLiveData.value =
                    permissionList.toTypedArray()
            }
        }
    }
}